﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml;
using System.Xml.XPath;

namespace rssappbar
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        public int visibleText = 1;
        public int firstCallFlag = 0;
        public int appbarset = 0;
        public int curNewsItem;
        public List<string> itemList = new List<string>();
        public List<DateTime> pubDateList = new List<DateTime>();
        public string baseDir;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void setContextMenu()
        {
            ContextMenu myMenu = new ContextMenu();
            MenuItem mySettingsContext = new MenuItem();
            mySettingsContext.Header = "Settings";
            mySettingsContext.Click += new RoutedEventHandler(mySettingsContext_Click);
            myMenu.Items.Add(mySettingsContext);
            myGrid.ContextMenu = myMenu;
        }

        void mySettingsContext_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindow settingsWindow = new SettingsWindow();
            settingsWindow.Show();
        }

        public void CrossFade(TextBlock FadeDown, TextBlock FadeUp)
        {
            DoubleAnimation FadeDownAnimation = new DoubleAnimation();
            FadeDownAnimation.From = 1.0;
            FadeDownAnimation.To = 0;
            FadeDownAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
            FadeDownAnimation.AutoReverse = false;

            DoubleAnimation FadeUpAnimation = new DoubleAnimation();
            FadeUpAnimation.From = 0;
            FadeUpAnimation.To = 1.0;
            FadeUpAnimation.Duration = new Duration(TimeSpan.FromSeconds(2));
            FadeUpAnimation.AutoReverse = false;

            FadeDown.BeginAnimation(TextBlock.OpacityProperty, FadeDownAnimation);
            FadeUp.BeginAnimation(TextBlock.OpacityProperty, FadeUpAnimation);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (firstCallFlag == 0)
            {
                SetAppBar();
            }
        }

        private void SetAppBar()
        {
            AppBarFunctions.SetAppBar(this, ABEdge.Bottom);
            appbarset=1;
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (curNewsItem == itemList.Count)
            {
                curNewsItem = 0;
            }
            if (visibleText == 1)
            {
                textBlock2.Text = itemList[curNewsItem];
                curNewsItem++;
                CrossFade(textBlock1, textBlock2);
                visibleText = 2;
                return;
            }
            if (visibleText == 2)
            {
                textBlock1.Text = itemList[curNewsItem];
                curNewsItem++;
                CrossFade(textBlock2, textBlock1);
                visibleText = 1;
                return;
            }
        }

        private void setFirstValues()
        {
            bool IntCon = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

            textBlock1.Opacity = 1;
            textBlock2.Opacity = 0;

            if (IntCon != false)
            {

                curNewsItem = 0;
                GetXMLFile();
                GetXMLData();
                textBlock1.Text = itemList[curNewsItem];
                curNewsItem++;
                textBlock2.Text = itemList[curNewsItem];
                curNewsItem++;
                firstCallFlag = 1;
            }
            else 
            {
                textBlock1.Text = "Network Connection Not Available";
                textBlock2.Text = "Network Connection Not Available";
            }
        }

        public void GetXMLFile()
        {
            System.Reflection.Assembly a = System.Reflection.Assembly.GetEntryAssembly
();
            baseDir = System.IO.Path.GetDirectoryName(a.Location);

            WebClient Client = new WebClient();
            Client.DownloadFile(Properties.Settings.Default.RSSFeed, baseDir + @"\rssfeed.xml");
        }

        public void GetXMLData()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(baseDir + @"\rssfeed.xml");

            XmlNodeList nodeListItems;
            XmlNodeList dateListItems;

            string myString = "/rss/channel/item/description/text()";
            string myDateString = "/rss/channel/item/pubDate/text()";
            nodeListItems = doc.SelectNodes(myString);
            dateListItems = doc.SelectNodes(myDateString);
            itemList.Clear();
            itemList.Add("Latest Science News Headlines from the BBC");
            int listlen = nodeListItems.Count;
            for (int i = 0; i < listlen; i++)
            {
                string myNodeString = dateListItems[i].InnerText;
                int myStrLen = myNodeString.Length;
                string myTrimmedString = myNodeString.Remove(myStrLen - 4, 4);
                myTrimmedString = myTrimmedString.Remove(0, 5);
                DateTime myDate = DateTime.ParseExact(myTrimmedString, "dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                DateTime minDate = DateTime.Now.AddDays(-Properties.Settings.Default.ShowDays);
                if (myDate > minDate)
                {
                    itemList.Add(nodeListItems[i].InnerText);
                }
            }
        }

        private void image1_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void fillLabels()
        {
            if (firstCallFlag == 0 & appbarset==1)
            {
                setFirstValues();
                DispatcherTimer dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Interval = new TimeSpan(0, 0, Properties.Settings.Default.DelayDuration);
                dispatcherTimer.Start();
            }
        }

        private void myMainWindow_LocationChanged(object sender, EventArgs e)
        {
            if (firstCallFlag == 0)
            {
                setContextMenu();
                fillLabels();
            }
        }
    }
}
